#include "widget.h"
#include "idcard_helper.h"

#include <qboxlayout.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <QDebug>
#include <QDate>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // 整体采用垂直布局
    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
    verticalLayout->setSpacing(0);
    verticalLayout->setContentsMargins(0, 0, 0, 0);

    QHBoxLayout *inputLayout = new QHBoxLayout();
    inputLayout->setSpacing(0);
    inputLayout->setContentsMargins(0, 0, 0, 0);

    QFont font = QFont("Inter", 12, false);

    lineEditIdCardNo = new QLineEdit(this);
    QValidator* regExpValidator = new QRegExpValidator(QRegExp("[(0-9){17}|(X0-9)]+$"));
    lineEditIdCardNo->setValidator(regExpValidator);
    lineEditIdCardNo->setMinimumWidth(280);
    lineEditIdCardNo->setFixedHeight(40);
    lineEditIdCardNo->setPlaceholderText("请输入18位身份证号");
    lineEditIdCardNo->setFont(font);
    lineEditIdCardNo->setMaxLength(18);
    inputLayout->addWidget(lineEditIdCardNo);

    btnValidate = new QPushButton(this);
    btnValidate->setFixedHeight(40);
    btnValidate->setText("验证");
    btnValidate->setFont(font);
    inputLayout->addWidget(btnValidate);

    verticalLayout->addLayout(inputLayout);

    connect(btnValidate, &QPushButton::clicked, this, &Widget::handleValidate);
}

Widget::~Widget() {}

void Widget::handleValidate() {
    QMessageBox msgBox;
    QString idCardNo = lineEditIdCardNo->displayText();
    QString errorMsg = validateIdCardNoNotEmpty(idCardNo);

    if (!errorMsg.isEmpty()) {
        msgBox.setText(errorMsg);
        msgBox.exec();
        return;
    }

    auto localIsValidIdCardNo = IdCardHelper::isValidIdCardNo(idCardNo);
    if (localIsValidIdCardNo.isValid) {
        qDebug() << "is idCardNo[" << idCardNo << "] valid: " << localIsValidIdCardNo.isValid;
        msgBox.setText("身份证号格式正确");
        msgBox.exec();
        return;
    } else {
        msgBox.setText(localIsValidIdCardNo.notValidReason);
        msgBox.exec();
        return;
    }

}

QString Widget::validateIdCardNoNotEmpty(QString &idCardNo) {
    qDebug() << "idCardNo is: " << idCardNo;
    if (idCardNo.isNull() || idCardNo.isEmpty()) {
        return "请输入身份证号!";
    } else {
        return "";
    }
}

IdCardHelper::IdCardEntity IdCardHelper::isValidIdCardNo(QString &idCardNo) {
    if (idCardNo.isNull() || idCardNo.trimmed().isEmpty() || idCardNo.length() != 18) {
        IdCardEntity idCardEntity{idCardNo, false, "身份证号长度不正确"};
        return idCardEntity;
    }

    QString provinceNo = idCardNo.mid(0, 2);
    QString birthdayNo = idCardNo.mid(6, 8);
    int year = birthdayNo.midRef(0, 4).toInt();
    int month = birthdayNo.midRef(4, 2).toInt();
    int dayOfMonth = birthdayNo.midRef(6, 2).toInt();
    QString lastChar = idCardNo.mid(17);

    qDebug() << "省份代码[provinceNo=" << provinceNo
             << "],出生年月日代码[birthdayNo=" << birthdayNo
             << "],最后一位代码[lastChar=" << lastChar << "]"
             << "year: " << year
             << "month: " << month
             << "dayOfMonth: " << dayOfMonth;

    bool isValidDate = IdCardHelper::checkValidDate(year, month, dayOfMonth);
    if (!isValidDate) {
        IdCardEntity idCardEntity{idCardNo, isValidDate, "身份证日期不正确"};
        return idCardEntity;
    }

    char calculateLastChar = IdCardHelper::calculateLastChar(idCardNo);
    bool isValid = calculateLastChar == lastChar;

    if (!isValid) {
        qDebug() << "身份证号[idCardNo="
                 << idCardNo
                 << "],计算出的最后一位[calculateLastChar="
                 << calculateLastChar
                 << "],此身份证号不正确";
        IdCardEntity idCardEntity = IdCardEntity{idCardNo, isValid, "身份证号最后一位不正确"};
        return idCardEntity;
    }
    IdCardEntity idCardEntity{idCardNo, true, ""};
    return idCardEntity;
}
